package org.dromara.xxmj.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.xxmj.domain.CourseOrder;
import org.dromara.xxmj.domain.dto.CourseOrderDto;
import org.dromara.xxmj.domain.vo.CourseOrderVo;
import org.dromara.xxmj.service.ICourseOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程订单控制器
 *
 * @author Vincent
 */
@Slf4j
@Tag(name = "课程订单管理API")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xxmj/order")
public class CourseOrderController extends BaseController {

    @Resource
    private ICourseOrderService orderService;

    /**
     * 创建课程订单
     */
    @Operation(summary = "创建课程订单")
    @PostMapping("/create")
    public R<Map<String, Object>> createOrder(@RequestBody @Valid CourseOrderDto dto) {
        Long orderId = orderService.createOrder(dto);
        CourseOrder order = orderService.getOrderById(orderId);

        Map<String, Object> result = new HashMap<>();
        result.put("id", order.getId());
        result.put("orderNo", order.getOrderNo());
        result.put("amount", order.getAmount());

        return R.ok(result);
    }

    /**
     * 获取订单详情
     */
    @Operation(summary = "获取订单详情")
    @GetMapping("/detail/{id}")
    public R<CourseOrder> getOrderDetail(@Parameter(description = "订单ID") @PathVariable Long id) {
        return R.ok(orderService.getOrderById(id));
    }

    /**
     * 支付订单
     */
    @Operation(summary = "支付订单")
    @PostMapping("/pay/{id}")
    public R<Boolean> payOrder(
        @Parameter(description = "订单ID") @PathVariable Long id,
        @Parameter(description = "支付方式") @RequestParam(required = false, defaultValue = "1") String payType
    ) {
        return R.ok(orderService.payOrder(id, payType));
    }

    /**
     * 取消订单
     */
    @Operation(summary = "取消订单")
    @PostMapping("/cancel/{id}")
    public R<Boolean> cancelOrder(@Parameter(description = "订单ID") @PathVariable Long id) {
        return R.ok(orderService.cancelOrder(id));
    }

    /**
     * 订单列表
     * @Author 王晨沣 2307105054
     * @Date 2025-05-26 09:35:06
     */
    @Operation(summary = "订单列表")
    @GetMapping("/orderList")
    public R<List<CourseOrderVo>> listOrders(PageQuery pageQuery) {
        return R.ok(orderService.listOrders(pageQuery));
    }




}
